package com.tian.service;

import com.tian.dto.LoanFeeReqDto;
import com.tian.dto.LoanFeeRespDto;
import com.tian.entity.LoanApply;
import com.tian.entity.LoanProductInfo;
import com.tian.entity.PayTypeFeeConfig;
import com.tian.enums.CalculateTypeEnum;
import com.tian.mapper.LoanApplyMapper;
import com.tian.mapper.LoanProductInfoMapper;
import com.tian.mapper.PayTypeFeeConfigMapper;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年10月09日 21:56
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 * <p>
 * 费用计算
 */
@Service(version = "1.0.0")
public class LoanFeeServiceImpl implements LoanFeeService {

    @Resource
    private PayTypeFeeConfigMapper payTypeFeeConfigMapper;
    @Resource
    private LoanApplyMapper loanApplyMapper;
    @Resource
    private LoanProductInfoMapper loanProductInfoMapper;

    @Override
    public LoanFeeRespDto calculate(LoanFeeReqDto loanFeeReqDto) {
        LoanFeeRespDto loanFeeRespDto = new LoanFeeRespDto();

        String custNo = loanFeeReqDto.getCustNo();

        if (loanFeeReqDto.getCalculateType() == null || CalculateTypeEnum.ALL.getType() == loanFeeReqDto.getCalculateType()) {

            BigDecimal payTypeFeeAmount = this.getPayTypeFeeAmount(loanFeeReqDto);

            BigDecimal loanProductFeeAmount = this.getLoanProductFeeAmount(loanFeeReqDto);

            BigDecimal totalFeeAmount = loanProductFeeAmount.add(payTypeFeeAmount);

            loanFeeRespDto.setTotalFee(totalFeeAmount);
            loanFeeRespDto.setAmount(loanFeeReqDto.getAmount());
            loanFeeRespDto.setCustNo(custNo);
            return loanFeeRespDto;
        }
        if (CalculateTypeEnum.PAY_TYPE.getType() == loanFeeReqDto.getCalculateType()) {
            loanFeeRespDto.setTotalFee(getPayTypeFeeAmount(loanFeeReqDto));
            loanFeeRespDto.setAmount(loanFeeReqDto.getAmount());
            loanFeeRespDto.setCustNo(custNo);
            return loanFeeRespDto;
        }
        if (CalculateTypeEnum.LOAN_PRODUCT.getType() == loanFeeReqDto.getCalculateType()) {
            loanFeeRespDto.setTotalFee(getLoanProductFeeAmount(loanFeeReqDto));
            loanFeeRespDto.setAmount(loanFeeReqDto.getAmount());
            loanFeeRespDto.setCustNo(custNo);
            return loanFeeRespDto;
        }

        return null;
    }

    private BigDecimal getLoanProductFeeAmount(LoanFeeReqDto loanFeeReqDto) {
        /**
         *
         *  通过custNo 查询用户贷款表 获取 loan_info_id
         * 在通过 loan_info_id 去查询  贷款产品表  fee费率
         */
        LoanApply loanApply = loanApplyMapper.selectByCustomerNo(loanFeeReqDto.getCustNo());
        if (loanApply == null) {
            System.out.println("客户没有贷款异常"+loanFeeReqDto.getCustNo());
            throw new RuntimeException("客户没有贷款");

        }

        Integer productId = loanApply.getProductId();
        LoanProductInfo loanProductInfo = loanProductInfoMapper.selectByPrimaryKey(productId);
        //贷款产品费率
        return loanFeeReqDto.getAmount().multiply(loanProductInfo.getFee());
    }

    private BigDecimal getPayTypeFeeAmount(LoanFeeReqDto loanFeeReqDto) {
        Integer payType = loanFeeReqDto.getPayType();
        /**
         *   payType查询支付方式----费率
         */
        PayTypeFeeConfig payTypeFeeConfig = payTypeFeeConfigMapper.selectByPayType(payType);
        if (payTypeFeeConfig == null) {
            throw new RuntimeException("支付方式不对，payType="+payType);
        }
        //支付方式费用
        return loanFeeReqDto.getAmount().multiply(payTypeFeeConfig.getFee());
    }
}
